require 'base64'
require 'fileutils'
require 'etc'
require File.dirname(__FILE__) + '/.config.rb'

module PodDownload
    # 检查库是否存在
    def PodDownload.check_lib_exist(name)
        podFilePath = LOCAL_REP_PATH + '/' + name + '/'
        if File.directory?(podFilePath)
            puts("本地已经存在库：#{name}")
          return true
        else
          puts("#{LOCAL_REP_PATH}路径下未找到 #{name}")
          return false
        end
    end
    
    # 尝试检出库
    def PodDownload.try_check_out(name)
        puts("#{name}开始clone...")
        #TODO:checkout master存在风险
        username = Etc.getlogin()
        time =  Time.now.strftime("%Y%m%d%H%M%S")
        branchName = "dev/#{username}_#{time}"
        puts("#{name} checkout branch name is #{branchName}")
        log = `cd;cd #{LOCAL_REP_RELATIVE_HOME_PATH};git clone #{REMOTE_REP_PATH}/#{name}.git;cd #{name};git checkout master;git pull;git checkout -b #{branchName}`
        puts log
        if $? != 0 then
          puts("#{name} download failed!!!")
        else
          puts("#{name} download success!!")
        end
    end
    
end

